package com.cse.htmlparser.refactory.jingdong;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.NodeVisitor;
import org.json.JSONException;
import org.json.JSONObject;
import com.cse.core.Product;
import com.cse.htmlparser.AbstractParser;

public class JingDong extends AbstractParser{
	
	public JingDong(String html,String file){
		super(html, file);
	}

	@Override
	protected void extract() {
		// TODO Auto-generated method stub
		initialize(htmlpath);
		NodeFilter brandfilter = new HasAttributeFilter("brand");
		try{
			NodeList list = parser.extractAllNodesThatMatch(brandfilter);
			NodeIterator iterator = list.elements();

			while(iterator.hasMoreNodes()){
				Node node = iterator.nextNode();
				if(node.getClass() == Div.class){
					node.accept(new NodeVisitor(){

						@Override
						public void visitTag(Tag tag) {
							// TODO Auto-generated method stub
							super.visitTag(tag);
							if(tag.getClass() == LinkTag.class){
								String name = tag.getAttribute("title");
								String link = tag.getAttribute("href");
								map.put(name, link);
							}
						}
					});
				}
			}	
			WriteFile();
		}catch(Exception e){
			logger.error(e);
		}
	}

	@Override
	protected void extractbrand(String brand, String url) {
		// TODO Auto-generated method stub
		initialize(url);
		logger.info("执行了extractbrand") ;
		NodeFilter ulFilter = new HasAttributeFilter("class","list-h");
		try{
			Node node = parser.extractAllNodesThatMatch(ulFilter).elementAt(0);
			NodeList children = node.getChildren();
			NodeIterator iterator = children.elements();
			final Product product = new Product();
			
			while(iterator.hasMoreNodes()){
				Node child = iterator.nextNode();
				product.setCategory("phone");
				product.setBrand(brand);
				child.accept(new NodeVisitor() {

					@Override
					public void visitTag(Tag tag) {
						// TODO Auto-generated method stub
						super.visitTag(tag);
						if(tag.getClass() == ImageTag.class)
						{
							String src = tag.getAttribute("src");
							if(src == null){
								src = tag.getAttribute("data-lazyload");
							}
							logger.info("图片地址:"+src);
							product.setImageURI(src);
							String href = ((Tag)tag.getParent()).getAttribute("href");
							product.setOriginalUrl(href);
							logger.info("商品链接:"+href);
							String content = tag.getAttribute("alt");
							
							logger.info("商品内容 ："+content);
							product.setContent(content);
							String price = getPrice(href);
							logger.info("商品价格:"+price);
							product.setPrice(price);
						}
						
					}
				});
				
			}
			product.setUpdatedtime(System.nanoTime()+"");
			indexer.addProduct(product);
		}catch(Exception exception){
			exception.printStackTrace();
		}
	}

	@Override
	protected void extractall() {
		// TODO Auto-generated method stub
		try{
			Map<String, String> map = getMap();
			Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
			while(iterator.hasNext()){
				Entry<String, String> entry = iterator.next();
				String brand = entry.getKey();
				String url = entry.getValue();
				String text = getHtml(url);
				extractbrand(brand, text);
			}
			indexer.optimizeIndex();
		}catch(Exception exception){
			exception.printStackTrace();
		}finally{
			indexer.close();
		}
		
	}
	
	public String getHtml(String myurl){
		StringBuffer sb = new StringBuffer();  ;
		 try {  
	            URL url = new URL(myurl);  
	            BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream(),"gbk"));  
	            
	            String s=null;  
	            while((s=br.readLine())!=null)  
	            {  
	                sb.append(s+"/r/n");  
	            }  
	        
	        } catch (MalformedURLException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        } catch (UnsupportedEncodingException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }
		return sb.toString(); 
	          
	}
	
	
	/**
	 * 
	 * @param url 京东得到价格的api
	 * @return 该商品的价格
	 */
	public String getPrice(String url){
		String id = url.substring(19, url.length()-5);
		String priceURL = "http://p.3.cn/prices/mgets?skuIds=J_"+id+"&type=1&area=1_72_4137";
		String tmp = getHtml(priceURL);
		String json = tmp.substring(1,tmp.length()-1);
		String price = null;
		try {
			JSONObject jsonObject = new JSONObject(json);
			price  = jsonObject.getString("p");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return price;
	}
	
	public static void main(String[] args)
	{
		JingDong jingDong = new JingDong(".\\html\\jingdongphone.htm", ".\\items\\jingdong\\phone");
		//jingDong.extract();
		jingDong.startExtract();
	}
}
